
/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-02 11:27:13
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-02-01 10:01:42
 */
import bagCtr from "../ctrs/bags/bagCtr";
import barrackCtr from "../ctrs/barracks/barrackCtr";
import gameCtr from "../ctrs/gameCtr";
import guideCtr from "../ctrs/guide/guideCtr";
import GuideData from "../ctrs/guide/guideCtr";
import helpCtr from "../ctrs/helps/helpCtr";
import netCtr from "../ctrs/netCtr";
import pvpCtr from "../ctrs/pvp/pvpCtr";
import treasureCtr from "../ctrs/treasure/treasureCtr";
import BaseGameScene from "../libs/core/BaseGameScene";
import { GameFacade } from "../libs/core/GameFacade";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { drawScroll2 } from "../libs/core/utils";
import { SceneManager } from "../libs/manager/SceneManager";
import { INotification } from "../libs/pureMvc/Interfaces";
import * as sdk from "../libs/sdks/sdk";
const { ccclass, property } = cc._decorator;

@ccclass

export default class startScene extends BaseGameScene {

  @property({ type: cc.Button, tooltip: "冒险按钮" })
  rishBtn: cc.Button = null;;

  @property({ type: cc.Button, tooltip: "对战按钮" })
  warBtn: cc.Button = null;

  @property({ type: cc.Prefab, tooltip: "miner预制体" })
  minerPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "spearman预制体" })
  spearmanPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "guard预制体" })
  guardPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "shield预制体" })
  shieldPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "castle预制体" })
  castlePre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "militia预制体" })
  militiaPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "bowman预制体" })
  bowmanPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "swordsman预制体" })
  swordsmanPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "cavalry预制体" })
  cavalryPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "trapped预制体" })
  trappedPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "dogs预制体" })
  dogsPre: cc.Prefab = null;


  @property({ type: cc.Prefab, tooltip: "master预制体" })
  masterPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "fighter预制体" })
  fighterPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "warlock预制体" })
  warlockPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "scarecrow预制体" })
  scarecrowPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "jumpFire预制体" })
  jumpFirePre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "logistics预制体" })
  logisticsPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "树预制体" })
  treePre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "矿预制体" })
  mineralPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "圈预制体" })
  circlePre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "圈预制体" })
  circle2Pre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "三角预制体" })
  trianglePre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "四边预制体" })
  squarePre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "圈预制体2" })
  circle3Pre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "线预制体" })
  linePre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "怪物1预制体" })
  mon1Pre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "怪物2预制体" })
  mon2Pre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "怪物3预制体" })
  mon3Pre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "怪物4预制体" })
  mon4Pre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "加农炮预制体" })
  cannonPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "子弹预制体" })
  bulletPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "火球预制体" })
  fireBallPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "地雷预制体" })
  shellPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "地雷预制体" })
  dlPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "死亡" })
  diePre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "爆炸" })
  baozhaPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "地刺" })
  dcPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "冰冻" })
  frozenPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "回复" })
  recoverPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "服务器箭" })
  timeArrow: cc.Prefab = null;

  @property({ type: cc.Node, tooltip: "列表内容节点" })
  content: cc.Node = null;

  @property({ type: cc.Prefab, tooltip: "图标预制体" })
  itemPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "箭预制体" })
  arrowPre: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "奖励预制体" })
  rewardItem: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "奖励预制体" })
  rewardItem2: cc.Prefab = null;

  @property({ type: cc.Node, tooltip: "宝箱节点" })
  bxNode: cc.Node = null;

  @property({ type: cc.Node, tooltip: "列表节点" })
  listNode: cc.Node = null;

  @property({ type: cc.Node, tooltip: "主节点" })
  mainNode: cc.Node = null;

  @property({ type: cc.Label, tooltip: "用户昵称" })
  nickName: cc.Label = null;

  @property({ type: cc.Label, tooltip: "用户id" })
  roleId: cc.Label = null;

  @property({ type: cc.Label, tooltip: "用户钻石数量" })
  diamonds: cc.Label = null;

  @property({ type: cc.Sprite, tooltip: "头像" })
  avatar: cc.Sprite = null;

  @property({ type: cc.JsonAsset, tooltip: "英雄配置文件" })
  heroConf: cc.JsonAsset = null;

  @property({ type: cc.JsonAsset, tooltip: "宝箱配置文件" })
  treasureConf: cc.JsonAsset = null;

  @property({ type: cc.JsonAsset, tooltip: "宝箱兑换配置文件" })
  exchangeConf: cc.JsonAsset = null;

  @property({ type: cc.JsonAsset, tooltip: "pvp累胜奖励配置文件" })
  victoryConf: cc.JsonAsset = null;

  @property({ type: cc.JsonAsset, tooltip: "引导配置文件" })
  guideConf: cc.JsonAsset = null;

  @property({ type: cc.JsonAsset, tooltip: "遗物系统文件" })
  bagConf: cc.JsonAsset = null;

  @property({ type: cc.JsonAsset, tooltip: "助力系统文件" })
  helpConf: cc.JsonAsset = null;

  @property({ type: cc.Sprite, tooltip: "段位图标" })
  lvIcon: cc.Sprite = null;

  @property({ type: cc.Label, tooltip: "段位描述" })
  rankTxt: cc.Label = null;

  @property({ type: cc.Label, tooltip: "累胜描述" })
  victoryTxt: cc.Label = null;

  @property({ type: cc.Label, tooltip: "段位积分" })
  integralTxt: cc.Label = null;

  @property({ type: cc.ProgressBar, tooltip: "段位进度条" })
  rankPro: cc.ProgressBar = null;

  @property({ type: cc.Node, tooltip: "累胜宝箱节点" })
  victoryNode: cc.Node = null;

  @property({ type: cc.Prefab, tooltip: "累胜item" })
  victoryItem: cc.Prefab = null;

  @property({ type: cc.Prefab, tooltip: "飞字" })
  flyTxt: cc.Prefab = null;

  public selectKey: string = "1";

  onLoad(): void {
    super.onLoad();
    gameCtr.ins.curScene = this.node;
    GameFacade.getInstance().init();

    sdk.ins().init();

    cc.game.addPersistRootNode(cc.find("loadNode"));
    cc.game.addPersistRootNode(cc.find("tipLayer"));

    gameCtr.ins.initPool("castle", 5, this.castlePre);
    gameCtr.ins.initPool("miner", 10, this.minerPre);
    gameCtr.ins.initPool("spearman", 10, this.spearmanPre);
    gameCtr.ins.initPool("guard", 10, this.guardPre);
    gameCtr.ins.initPool("shield", 10, this.shieldPre);
    gameCtr.ins.initPool("militia", 10, this.militiaPre);
    gameCtr.ins.initPool("bowman", 10, this.bowmanPre);
    gameCtr.ins.initPool("swordsman", 10, this.swordsmanPre);

    gameCtr.ins.initPool("cavalry", 10, this.cavalryPre);
    gameCtr.ins.initPool("trapped", 10, this.trappedPre);
    gameCtr.ins.initPool("dogs", 10, this.dogsPre);
    gameCtr.ins.initPool("master", 10, this.masterPre);
    gameCtr.ins.initPool("fighter", 10, this.fighterPre);
    gameCtr.ins.initPool("warlock", 10, this.warlockPre);
    gameCtr.ins.initPool("scarecrow", 10, this.scarecrowPre);
    gameCtr.ins.initPool("jumpFire", 10, this.jumpFirePre);
    gameCtr.ins.initPool("logistics", 10, this.logisticsPre);


    gameCtr.ins.initPool("rewardItem", 20, this.rewardItem);
    gameCtr.ins.initPool("rewardItem2", 20, this.rewardItem2);
    gameCtr.ins.initPool("mon1", 10, this.mon1Pre);
    gameCtr.ins.initPool("mon2", 10, this.mon2Pre);
    gameCtr.ins.initPool("mon3", 10, this.mon3Pre);
    gameCtr.ins.initPool("mon4", 10, this.mon4Pre);
    gameCtr.ins.initPool("triangle", 5, this.trianglePre);
    gameCtr.ins.initPool("square", 5, this.squarePre);
    gameCtr.ins.initPool("circle", 5, this.circlePre);
    gameCtr.ins.initPool("circle2", 5, this.circle2Pre);
    gameCtr.ins.initPool("circle3", 5, this.circle3Pre);
    gameCtr.ins.initPool("line", 5, this.linePre);
    gameCtr.ins.initPool("tree", 10, this.treePre);
    gameCtr.ins.initPool("mineral", 10, this.mineralPre);
    gameCtr.ins.initPool("die", 20, this.diePre);
    gameCtr.ins.initPool("bullet", 10, this.bulletPre);
    gameCtr.ins.initPool("cannon", 10, this.cannonPre);
    gameCtr.ins.initPool("baozha", 10, this.baozhaPre);
    gameCtr.ins.initPool("dc", 10, this.dcPre);
    gameCtr.ins.initPool("shell", 10, this.shellPre);
    gameCtr.ins.initPool("dl", 10, this.dlPre);
    gameCtr.ins.initPool("arrow", 10, this.arrowPre);
    gameCtr.ins.initPool("fireBall", 10, this.fireBallPre);
    gameCtr.ins.initPool("frozen", 5, this.frozenPre);
    gameCtr.ins.initPool("recover", 5, this.recoverPre);

    gameCtr.ins.initPool("timeArrow", 10, this.timeArrow);

    gameCtr.ins.initPool("flyTxt", 10, this.flyTxt);

    this.initWindowsLayer();

    let json = this.heroConf.json;

    barrackCtr.ins.initTp(json);

    treasureCtr.ins.initTp(this.treasureConf.json, this.exchangeConf.json);

    gameCtr.ins.initTp(this.victoryConf.json);

    bagCtr.ins.initTp(this.bagConf.json);

    helpCtr.ins.initTp(this.helpConf.json);

    guideCtr.ins.guideJosn = this.guideConf.json;

    guideCtr.ins.initConfData();

    netCtr.ins.login().then(() => {

      let list = gameCtr.ins.victoryDic.getValueList();

      drawScroll2(this.victoryNode, list, this.victoryItem, "victoryCom", 7, 15);

      guideCtr.ins.guideIndex = gameCtr.ins.guideId;

      if (guideCtr.ins.guideIndex == 1) {

        SceneManager.getInstance().pushLayer("guideLayer");
      }
    });

    this.drawList();

  }

  /**
   * 预加载弹窗界面资源
   */
  public initWindowsLayer() {
    SceneManager.getInstance().preloadLayer("detailLayer", null, null, false); //预加载详情界面 防止引导时 卡住
    SceneManager.getInstance().preloadLayer("emailLayer", null, null, false);
    SceneManager.getInstance().preloadLayer("headLayer", null, null, false);
    SceneManager.getInstance().preloadLayer("noticeLayer", null, null, false);
    SceneManager.getInstance().preloadLayer("rankLayer", null, null, false);
    SceneManager.getInstance().preloadLayer("signLayer", null, null, false);
    SceneManager.getInstance().preloadLayer("tipLayer", null, null, false);
    SceneManager.getInstance().preloadLayer("bagLayer", null, null, false);
  }

  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_ZBCOM_UDPATE, PureMVCEvents.ON_ZBCOM_OPEN, PureMVCEvents.ON_USER_DATA];
  }

  public handleNotification(notification: INotification): void {
    let etName = notification.getName();
    switch (etName) {
      case PureMVCEvents.ON_ZBCOM_UDPATE:
        this.drawList();
        break;
      case PureMVCEvents.ON_ZBCOM_OPEN:
        this.onZbClick(null, true);
        break;
      case PureMVCEvents.ON_USER_DATA:
        this.drawUserUi();
        break;
    }
  }


  drawList() {
    let arr1 = gameCtr.ins.currentBarracks;
    this.content.removeAllChildren();
    drawScroll2(this.content, arr1, this.itemPre, "itemCom", 5, 20, 10);
  }

  onZbClick(e, key) {
    if (this.listNode.active && !key) {
      this.onBack(null, null);
      return;
    };
    this.bxNode.active = false;
    this.mainNode.active = false;
    this.listNode.active = true;
  }

  onBack(e, key) {
    this.bxNode.active = true;
    this.mainNode.active = true;
    this.listNode.active = false;
  }

  onHead() {
    SceneManager.getInstance().pushLayer("headLayer");
  }

  onToScene(e, key) {

    switch (key) {
      case "1":
        cc.find("loadNode").active = true;
        cc.find("loadNode").getChildByName("pp").active = false;

        SceneManager.getInstance().loadScene("mainScene", true, () => {
          cc.find("loadNode").active = false;
        });
        break;
      case "2":
        // if (!CC_DEBUG) {
        //   SceneManager.getInstance().pushLayer("tipLayer", "未开放!");
        //   return;
        // }
        //人机对战
        // SceneManager.getInstance().pushLayer("tipLayer", "匹配中...");
        let loadNode = cc.find("loadNode");
        loadNode.active = true;
        let pp = loadNode.getChildByName("pp");
        pp.active = true;
        this.scheduleOnce(() => {
          SceneManager.getInstance().loadScene("robotWarScene", true, () => {
            loadNode.active = false;
            pp.active = false;
          })
        }, 1);

        // this.scheduleOnce(() => {
        //   SceneManager.getInstance().loadScene("warScene", true, () => {
        //     loadNode.active = false;
        //     pp.active = false;
        //   })
        // }, 1);
        break;
      case "3":
        //人人对战
        cc.find("loadNode").active = true;
        SceneManager.getInstance().loadScene("warScene", true, () => {
          cc.find("loadNode").active = false;
        });
        break;
    }
  }

  /**
   * 绘制用户信息
   */
  public drawUserUi() {
    this.nickName.string = `${gameCtr.ins.nickName.substring(0, 8)}`;
    this.roleId.string = `UID:${gameCtr.ins.roleId}`;
    this.diamonds.string = `${gameCtr.ins.diamonds}`;

    this.rankTxt.string = "" + gameCtr.ins.rankTxt;
    this.victoryTxt.string = gameCtr.ins.victoryNum + "胜";
    this.integralTxt.string = "当前积分:" + gameCtr.ins.Integral;
    this.rankPro.progress = gameCtr.ins.rankPro;

    cc.resources.load(gameCtr.ins.lvIcon, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
      if (err) return;
      this.lvIcon.spriteFrame = sp;
    })

    let iconUrl = barrackCtr.ins.getBarrackById(Number(gameCtr.ins.avatar)).iconUrl;
    cc.resources.load(iconUrl, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
      if (err) return;
      this.avatar.spriteFrame = sp;
    })
  }

  /**
   * 点击邮箱
   */
  onEmailClick() {
    netCtr.ins.getEmailList();
  }

  /**
   * 点击邮箱
   */
  onSignClick() {
    SceneManager.getInstance().pushLayer("signLayer");
  }

  /**
   * 点击排名
   */
  onRankClick() {
    netCtr.ins.rankList();
  }

  /**
  * 点击背包
  */
  onBagClick() {
    SceneManager.getInstance().pushLayer("bagLayer");
    // SceneManager.getInstance().pushLayer("helpLayer");
  }
}

